package com.yan.responsibility_chain;

import java.util.Map;
import java.util.Objects;

public abstract class AbstractHandler implements Handler {

    private Handler nextHandler;
    private Integer handleLevel;
    private String department;

    AbstractHandler(String department, Integer handleLevel, Handler nextHandler) {
        super();
        this.department = department;
        this.handleLevel = handleLevel;
        this.nextHandler = nextHandler;
    }

    @Override
    public Integer getHandleLevel() {
        return handleLevel;
    }

    @Override
    public Handler getNextHandler() {
        return nextHandler;
    }

    @Override
    public void handle(Integer requiredLevel, String message) {
        Map<String, String> handleRemark = REMARK.get();
        if (getHandleLevel() >= requiredLevel) {
            if (handleRemark.isEmpty()) {
                System.out.printf("开始处理<%s>...\n", message);
            }
            handleRemark.put(department, "OK");
        }
        if (Objects.isNull(getNextHandler())) {
            if (handleRemark.size() == 0) {
                System.err.println("没人处理！！！！");
            } else {
                report();
                System.out.println("全部处理完成");
            }
            return;
        }
        getNextHandler().handle(requiredLevel, message);
    }
}
